#!/bin/bash
set -e

# version check: https://github.com/shssoichiro/oxipng/releases
OXIPNG_VERSION="9.1.2"
dpkgArch="$(dpkg --print-architecture)"

case "${dpkgArch##*-}" in
    amd64) OXIPNG_FILE="oxipng-${OXIPNG_VERSION}-x86_64-unknown-linux-musl.tar.gz"; OXIPNG_HASH='211d53f3781be4a71566fbaad6611a3da018ac9b22d500651b091c2b42ebe318' ;;
    arm64) OXIPNG_FILE="oxipng-${OXIPNG_VERSION}-aarch64-unknown-linux-musl.tar.gz"; OXIPNG_HASH='818d47d7195e1e0c4d58a9f3b6fd84aa3cd21770c60c876e73e2e6a17ca69b52' ;;
    *) echo >&2 "unsupported architecture: ${dpkgArch}"; exit 1 ;;
esac

# Install other deps
apt -y -q install advancecomp jpegoptim libjpeg-turbo-progs

git clone --depth 1 --branch "3.08" https://github.com/Matthias-Wandel/jhead.git /tmp/jhead
cd /tmp/jhead && make && cp /tmp/jhead/jhead /usr/local/bin/jhead
cd / && rm -rf /tmp/jhead

mkdir /oxipng-install
cd /oxipng-install

wget -q https://github.com/shssoichiro/oxipng/releases/download/v${OXIPNG_VERSION}/${OXIPNG_FILE}
sha256sum ${OXIPNG_FILE}
echo "${OXIPNG_HASH} ${OXIPNG_FILE}" | sha256sum -c
tar --strip-components=1 -xzf $OXIPNG_FILE
cp -v ./oxipng /usr/local/bin
cd / && rm -fr /oxipng-install
